{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "from datasketch import MinHash, MinHashLSH\n",
    "data1 = ['这个', '程序', '代码', '太乱', '那个', '代码', '规范']\n",
    "data2 = ['这个', '程序', '代码', '不', '规范', '那个', '更', '规范']\n",
    "data3 = ['这个', '程序', '代码', '不', '规范', '那个', '规范', '些']\n",
    "#造三个句子"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'规范', '那个', '这个', '太乱', '代码', '程序'} <class 'set'>\n",
      "{'规范', '不', '那个', '更', '这个', '代码', '程序'} <class 'set'>\n",
      "{'规范', '不', '那个', '更', '这个', '代码', '程序'} <class 'set'>\n"
     ]
    }
   ],
   "source": [
    "s1 = set(data1)\n",
    "s2 = set(data2)\n",
    "s3=  set(data3)\n",
    "print(s1,type(s1))\n",
    "print(s2,type(s2))\n",
    "print(s2,type(s2))#转换成集合 目的是方便计算Jaccard相似度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "s1与s2的Jaccard相似度 0.625\n",
      "s1与s3的Jaccard相似度 0.625\n",
      "s2与s3的Jaccard相似度 0.75\n",
      "s1与自己的Jaccard相似度 1.0\n"
     ]
    }
   ],
   "source": [
    "print('s1与s2的Jaccard相似度',float(len(s1.intersection(s2)))/float(len(s1.union(s2))) )\n",
    "print('s1与s3的Jaccard相似度',float(len(s1.intersection(s3)))/float(len(s1.union(s3))) )\n",
    "print('s2与s3的Jaccard相似度',float(len(s2.intersection(s3)))/float(len(s2.union(s3))) )\n",
    "print('s1与自己的Jaccard相似度',float(len(s1.intersection(s1)))/float(len(s1.union(s1))) )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<datasketch.minhash.MinHash object at 0x7f8a648cf990> <class 'datasketch.minhash.MinHash'>\n",
      "<datasketch.minhash.MinHash object at 0x7f8a648cf290> <class 'datasketch.minhash.MinHash'>\n",
      "<datasketch.minhash.MinHash object at 0x7f8a648cf810> <class 'datasketch.minhash.MinHash'>\n"
     ]
    }
   ],
   "source": [
    "# 创建MinHash对象\n",
    "m1 = MinHash()\n",
    "m2 = MinHash()\n",
    "m3 = MinHash()\n",
    "for d in data1:\n",
    "    m1.update(d.encode('utf8'))\n",
    "for d in data2:\n",
    "    m2.update(d.encode('utf8'))\n",
    "for d in data3:\n",
    "    m3.update(d.encode('utf8'))\n",
    "print(m1,type(m1))\n",
    "print(m2,type(m2))\n",
    "print(m3,type(m3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<datasketch.lsh.MinHashLSH object at 0x7f8a65300750> <class 'datasketch.lsh.MinHashLSH'>\n",
      "近似的邻居（Jaccard相似度>0.5） ['m2', 'm1', 'm3']\n"
     ]
    }
   ],
   "source": [
    "# 创建LSH\n",
    "lsh = MinHashLSH(threshold=0.5, num_perm=128)#设置阈值为0.6\n",
    "lsh.insert(\"m2\", m2)\n",
    "lsh.insert(\"m3\", m3)\n",
    "print(lsh,type(lsh))\n",
    "lsh.insert(\"m1\",m1)\n",
    "result = lsh.query(m1)#查找与m1的Jaccard相似度\n",
    "print(\"近似的邻居（Jaccard相似度>0.5）\", result)#m2 m1 m3与m1的相似度都大于0.5  但是阈值再设置高一点  那就要筛选掉了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<datasketch.lsh.MinHashLSH object at 0x7f8a65222550> <class 'datasketch.lsh.MinHashLSH'>\n",
      "近似的邻居（Jaccard相似度>0.8） ['m1']\n"
     ]
    }
   ],
   "source": [
    "# 创建LSH\n",
    "lsh = MinHashLSH(threshold=0.8, num_perm=128)#设置阈值为0.6\n",
    "lsh.insert(\"m2\", m2)\n",
    "lsh.insert(\"m3\", m3)\n",
    "print(lsh,type(lsh))\n",
    "lsh.insert(\"m1\",m1)\n",
    "result = lsh.query(m1)#查找与m1的Jaccard相似度\n",
    "print(\"近似的邻居（Jaccard相似度>0.8）\", result)#果然筛选掉了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m1.jaccard(m1)#自己与自己的相似度显然是1了"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
